<template>
  <div>
    <editCourse :rule-form="ruleForm" :business-id="businessId" @release="releaseCourse" @addDraft="addDraft" />
  </div>
</template>

<script>
import editCourse from '@/views/account/merchant/compoents/editCourse'
import { getBoById, updateCourse } from '@/api/account'
export default {
  name: 'MerchantCourseEdit',
  components: {
    editCourse
  },
  data() {
    return {
      id: '',
      businessId: '',
      status: '',
      ruleForm: {
        courseLabel: [],
        teacherProfile: [],
        introducePictures: [],
        teacherProfile1: '',
        teacherProfile2: '',
        teacherProfile3: '',
        refundNote: '',
        purchaseInfo1: {
          refundNote: ''
        },
        purchaseInfo2: {
          refundNote: ''
        },
        matterFrom: {
          refundNote: ''
        }
      }
    }
  },
  computed: {

  },
  created() {
    this.businessId = this.$route.params['businessId']
    this.id = this.$route.params['id']
    this.init()
  },
  methods: {
    init() {
      // 获取单个课程
      getBoById({
        id: this.id
      }).then(res => {
        const data = res.data
        this.status = data.status
        this.ruleForm = data
        const teacherRem = data.teacherProfile ? data.teacherProfile.split(',') : []
        this.ruleForm.teacherProfile = teacherRem
        // 课程标签转换数组
        const arr = this.ruleForm.courseLabel ? this.ruleForm.courseLabel.split(',') : []
        const branchSchoolIds = data.branchSchoolIds ? data.branchSchoolIds.split(',') : []
        const introducePictures = data.introducePictures ? data.introducePictures.split(',') : []
        this.$set(this.ruleForm, 'courseLabel', arr)
        this.$set(this.ruleForm, 'branchSchoolIds', branchSchoolIds)
        this.$set(this.ruleForm, 'introducePictures', introducePictures)
      }).catch(() => {})
    },
    // 修改
    releaseCourse(form) {
      const data = Object.assign({}, form)
      // if (this.status === 4) {
      //   data.status = 3
      // } else {
      //   data.status = 2
      // }
      data.status = 2
      // data.introducePictures = data.introducePictures && data.introducePictures.length > 0 ? data.introducePictures.join(',') : ''
      // data.courseLabel = data.courseLabel && data.courseLabel.length > 0 ? data.courseLabel.join(',') : ''
      // data.branchSchoolIds = data.branchSchoolIds.length>0 ? data.branchSchoolIds.join(',') : ''
      // data.businessId = this.businessId
      data.businessId = this.businessId
      updateCourse(data).then(res => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      }).catch(() => {})
    },
    // 保存草稿
    addDraft(form) {
      const data = Object.assign({}, form)
      // data.introducePictures = data.introducePictures && data.introducePictures.length > 0 ? data.introducePictures.join(',') : ''
      // data.branchSchoolIds = data.branchSchoolIds && data.branchSchoolIds.length > 0 ? data.branchSchoolIds.join(',') : ''
      // data.courseLabel = data.courseLabel && data.courseLabel.length > 0 ? data.courseLabel.join(',') : ''
      // data.labelName = data.labelName && data.labelName.length > 0 ? data.labelName.join(',') : ''
      data.status = 1
      data.businessId = this.businessId
      updateCourse(data).then(res => {
        this.$message({
          message: '保存草稿成功',
          type: 'success'
        })
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.push('/educational/curriculum')
      }).catch(() => {})
    }
  }

}
</script>
